package com.qrcode.QRCode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class CreateQRcode {

	public static void main(String[] args) {
		Qrcode x = new Qrcode();
		x.setQrcodeErrorCorrect('M');// 纠错等级
		x.setQrcodeEncodeMode('B');// N代表数字,A代表a-Z,B代表其他字符
		x.setQrcodeVersion(7);//1-40级
		//设置内容,宽,高
		String qrData = "hello world! 你好,世界!";
		int width = 67+12*(7-1);
		int height = 67+12*(7-1);
		//设置图片参数
		BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D gs = bufferedImage.createGraphics();
		gs.setBackground(Color.WHITE);
		gs.setColor(Color.BLACK);
		gs.clearRect(0, 0, width, height);
		int pixoff = 2;// 偏移量 避免解析错误
		//内容转化为字节数组
		byte[] d = qrData.getBytes();
		//遍历绘制二维码
		if (d.length > 0 && d.length < 120) {
			boolean[][] s = x.calQrcode(d);
			for (int i = 0; i < s.length; i++) {
				for (int j = 0; j < s.length; j++) {
					if (s[j][i]) {
						gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
					}
				}
			}
		}
		gs.dispose();
		bufferedImage.flush();
		try {
			//生成图片文件
			ImageIO.write(bufferedImage, "png", new File("E:/qrcode.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
